/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import java.io.IOException;  
import java.util.Collection;  
import java.util.HashSet;
import javax.ejb.EJB;  
import javax.servlet.RequestDispatcher;  
import javax.servlet.ServletException;  
import javax.servlet.annotation.WebServlet;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;
import gestionnaires.GestionnaireDisques;
import java.util.ArrayList;
import modeles.Disque;

/**
 *
 * @author Eldryn
 */
@WebServlet(name = "ServletPanier", urlPatterns = {"/ServletPanier"})
public class ServletPanier extends HttpServlet {
    
    @EJB
    private GestionnaireDisques gestionnaireDisques;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occursidFilm
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        String idFilm = request.getParameter("idFilm");
        String forwardTo = "";
        String message = "";
        HttpSession session = request.getSession(true);
        Collection<Disque> panier = (Collection<Disque>)session.getAttribute("panier");
        Double total = (Double)session.getAttribute("total");
        String auth = (String)session.getAttribute("connecte");
       
        if(panier == null) {
            panier = new ArrayList<Disque>();
            session.setAttribute("panier", panier);
            
        }
         if (auth == null){
             auth="non";
             session.setAttribute("connecter", auth);
         }
        if(total == null){
                total = new Double(0);
                session.setAttribute("total", total);
        }
        if (action != null) {
            if (action.equals("ajouterAuPanier")){
                Disque film = gestionnaireDisques.getDisqueFromId(Integer.parseInt(idFilm));
                panier.add(film);
                session.setAttribute("panier", panier);
                total+=film.getPrix();
                session.setAttribute("total", total);
                message = "Ajout panier ok";
                RequestDispatcher dp = request.getRequestDispatcher("ServletDisques?action=listerLesDisques");  
                dp.forward(request, response);
                
                
            }else if(action.equals("afficherPanier")){
                forwardTo = "index.jsp?action=afficherPanier";
                
            }else if(action.equals("retirerDuPanier")){
                System.out.println(panier);
                Disque aEnlever =gestionnaireDisques.getDisqueFromId(Integer.parseInt(idFilm));
                panier.remove(aEnlever);
                total-=aEnlever.getPrix();
                session.setAttribute("total", total);
                forwardTo = "index.jsp?action=afficherPanier";
            }else if(action.equals("validerLaComande")){
                 System.out.println(auth);
               if(auth != "oui"){
                 forwardTo="index.jsp?action=afficherPanier&message=auth";
                }else if(auth == "oui"){
                    panier.clear();
                    total = new Double(0);
                session.setAttribute("total", total);
                    message = "panierValider";
                    forwardTo="index.jsp?action=content";
                }
                
                
            }
            
        }
            RequestDispatcher dp = request.getRequestDispatcher(forwardTo + "&message=" + message);
        //RequestDispatcher dp = request.getRequestDispatcher("ServletDisques?action=listerLesDisques");  
        dp.forward(request, response);  
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
